import { Base } from 'base.js';
import { Config } from 'config.js';
class Address extends Base{

  constructor(){
    super();
  }


  setAddressInfo(res){
    var province = res.provinceName || res.province,
      city = res.cityName || res.city,
      country = res.countyName || res.country,
      detail = res.detailInfo || res.detail;
    var totalDetail = city + country + detail;

    console.log(res);

    //直辖市，取出省部分
    if (!this.isCenterCity(province)) {
      totalDetail = province + totalDetail;
    };
    return totalDetail;
  }

  getAddressInfo(callback){
     var that = this;
     var param = {
       url :'address',
       sCallback:function(res){
         if(res){
           res.totalDetail = that.setAddressInfo(res);
           callback && callback(res);
         }
       }
     };
     this.request(param);
  }


  isCenterCity(name){
    var centerCitys = ['北京市','天津市','上海市','重庆市'],
    flag = centerCitys.indexOf(name) >= 0;
    return flag;
  }


  submitAddress(data,callback){
    data = this._setUpAddress(data);
    var param = {
      url : 'address',
      type: 'POST',
      data : data,
      sCallback:function(res){
        console.log("success");
        callback && callback(true,res);
      },
      eCallback:function(res){
        console.log("fail");
        callback && callback(false,res);
      }
    }
    console.log("callback");
    this.request(param);
  }

  _setUpAddress(res){
    var formData = {
      name : res.userName,
      province : res.provinceName,
      city : res.cityName,
      country: res.countyName, 
      mobile:res.telNumber,
      detail : res.detailInfo
    }
    return formData;
  }






}

export {Address}